$(function () {
    var lottery = {
        place: 0,
        click: false,
        index: -1,
        count: 0,
        timer: 0,
        speed: 20,
        times: 0,
        cycle: 50,
        prize: -1,
        init: function (id) {
            if ($("#" + id).find(".lottery-unit").length > 0) {
                $lottery = $("#" + id);
                $units = $lottery.find(".lottery-unit");
                this.obj = $lottery;
                this.count = $units.length;
                $lottery.find(".lottery-unit-" + this.index).addClass("active");
            }
            ;
        },
        roll: function () {
            var index = this.index, count = this.count, lottery = this.obj;
            $(lottery).find(".lottery-unit-" + index).removeClass("active");
            index += 1;
            if (index > count - 1) {
                index = 0;
            }
            ;
            $(lottery).find(".lottery-unit-" + index).addClass("active");
            this.index = index;
            return false;
        },
        stop: function () {
            lottery.times += 1;
            lottery.roll();
            if (lottery.times > lottery.cycle + 10 && lottery.prize == lottery.index) {
                clearTimeout(lottery.timer);
                lottery.prize = -1;
                lottery.times = 0;
                lottery.click = false;
                console.log(lottery.place);
                console.log('您抽中了第' + lottery.place + '个奖品');
                var txt = $(".lottery-unit.active").find("p").text();
                console.log(txt);
                $("body").find(".tipBox").fadeIn();
                if(lottery.place == 4){
                    $("body").find(".tipBox h4").css("visibility","hidden");
                    $("body").find(".tipBox h5 span").text(txt);
                }else{
                    $("body").find(".tipBox h4").css("visibility","initial");
                    $("body").find(".tipBox h5 span").text(txt);
                }


            } else {
                if (lottery.times < lottery.cycle) {
                    lottery.speed -= 10;
                } else if (lottery.times == lottery.cycle) {
                    lottery.place = Math.random() * (lottery.count) | 0;
                    lottery.prize = lottery.place;
                } else {
                    if (lottery.times > lottery.cycle + 10 && ((lottery.prize == 0 && lottery.index == 7) || lottery.prize == lottery.index + 1)) {
                        lottery.speed += 110;
                    } else {
                        lottery.speed += 20;
                    }
                }
                if (lottery.speed < 40) {
                    lottery.speed = 40;
                }
                ;
                lottery.timer = setTimeout(lottery.stop, lottery.speed);
            }
            return false;
        },
        getLottery: function () {
            lottery.speed = 100;
            lottery.stop();
            lottery.click = true;
        }
    };
    //开始抽奖
    $("#lottery .btn").on('click', function (event) {
        event.preventDefault();
        lottery.init('lottery');
        var nub = $(".frequency p span").text();
        if (lottery.click) {
            return false;
        }else{
            if(nub!=0){
                $(".frequency p span").text(nub-1);
                lottery.getLottery();
                return false;
            }else if(nub == 0){
                $(".frequency p span").text(0);
                $("body").find(".nothingBox").fadeIn(); //没有抽奖次数
            }
        }

    });
    //关闭弹窗
    $("body").on("click",".close",function(){
        $("body").find(".tipBox").fadeOut();
    });
    $("body").on("click",".confirmBtn",function(){
        $("body").find(".nothingBox").fadeOut();
    });

    //点击再次抽取
    $("body").on("click",".againBtn",function(event){
        $("body").find(".tipBox").fadeOut();
        var nub = $(".frequency p span").text();
        if(nub!=0){
            $(".frequency p span").text(nub-1);
            lottery.getLottery();
            return false;
        }else if(nub == 0){
            $(".frequency p span").text(0);
            $("body").find(".nothingBox").fadeIn();//没有抽奖次数
        }
    });
});
